import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:gzmall_app/brands/brands_details_page.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/fwebView_scaffold.dart';
import 'package:gzmall_app/common/launch_util.dart';
import 'package:gzmall_app/common/setting_util.dart';
import 'package:gzmall_app/common/share_util.dart';
import 'package:gzmall_app/common/toast_util.dart';
import 'package:gzmall_app/goods/goods_details_page.dart';
import 'package:gzmall_app/goods/goods_list_widget.dart';
import 'package:gzmall_app/goods/home_goods_list_page.dart';
import 'package:gzmall_app/home/actives_page.dart';
import 'package:gzmall_app/home/brand_list_page.dart';
import 'package:gzmall_app/home/clearance_list_page.dart';
import 'package:gzmall_app/home_page.dart';
import 'package:gzmall_app/mine/order_details.dart';
import 'package:gzmall_app/mine/order_page.dart';
import 'package:gzmall_app/model/store.dart';
import 'package:gzmall_app/store/near_store_list_page.dart';
import 'package:gzmall_app/store/store_detail_page.dart';
import 'package:gzmall_app/topup/topup_page.dart';

class OpenPageUtil {
  static openPage(
      BuildContext context, String type, String pageStr, String paramsStr,
      {String name,
      String latitude,
      String longitude,
      String adcode,
      String uid,
      String shareTitle,
      String shareContent,
      String shareAvatar,
      String showTips}) {
    Widget page;
    if (type != null &&
        type.isNotEmpty &&
        pageStr != null &&
        pageStr.isNotEmpty) {
      if (type == "app" || type == "Native") {
        switch (pageStr) {
          case "activity_page":
            page = HomeGoodsListPage(
              pageStr,
              name ?? '',
              params: paramsStr,
            );
            break;
          case "clearance_list_page":
            page = ClearanceListPage(name ?? '');
            break;
          case "day_goods_page":
            page = HomeGoodsListPage(
              pageStr,
              name ?? '',
              params: paramsStr,
            );
            break;
          case "near_store_list_page":
            page = NearStoreListPage(
              latitude: latitude,
              longitude: longitude,
              adcode: adcode,
            );
            break;
          case "yan_xuan_page":
            page = HomeGoodsListPage(
              pageStr,
              name,
              params: paramsStr,
            );
            break;
          case "active_page":
            page = ActivesPage(name ?? '', params: paramsStr);
            break;
          case "brands_page":
            page = BrandListPage(title: name ?? '', params: paramsStr);
            break;
          case "order_details":
            page = OrderDetailsPage(title: name ?? '', params: paramsStr);
            break;
          case "topup_page":
            page = TopupPage();
            break;
          case "order_page":
            Map<String, dynamic> params = json.decode(paramsStr);
            page = OrderPage(
              curIndex: params != null ? (params["index"] ?? 0) : 0,
            );
            break;
          case "goods_details_page":
            Map<String, dynamic> params = json.decode(paramsStr);
            page = GoodsDetailsPage(
              productId: params != null ? (params["id"] ?? 0) : 0,
            );
            break;
          case "goods_list_widget":
            Map<String, dynamic> params = json.decode(paramsStr);
            if (params != null) {
              page = GoodsListWidget(
                categoryId: params["id"],
                categoryName: params["name"],
                level: params["level"],
              );
            }
            break;
          case "store_detail_page":
            Map<String, dynamic> params = json.decode(paramsStr);
            Store store = Store(params["id"], '', '', '', '', '', '', 0.0, 0.0,
                '', '', [], false, '', '', '', '');
            page = StoreDetailPage(store: store);
            break;
          case "brands_details_page":
            Map<String, dynamic> params = json.decode(paramsStr);
            page = BrandsDetailsPage(id: params["id"]);
            break;
          case "home_goods_list_page":
            page = HomeGoodsListPage(
              pageStr ?? "",
              name ?? "",
              params: paramsStr ?? "",
            );
            break;
          case "category_widget":
            Map<String, dynamic> params = json.decode(paramsStr);
            if (params != null) {
              Navigator.of(context).pushAndRemoveUntil(
                  MaterialPageRoute(
                      builder: (context) => HomePage(
                            currentIndex: 1,
                            categoryRootId: params["root_id"],
                          )),
                  (route) => route == null);
            }
            return;
        }
      } else if (type == 'web' || type == "Web") {
        page = FWebviewScaffold(
          url: pageStr + "?uid=${uid ?? ''}",
          userAgent: Api.kWebViewUserAgent,
          appBar: AppBar(
            title: Text(name ?? ''),
          ),
          enableMessaging: true,
          withJavascript: true,
          clearCache: true,
        );
      } else if (type == "Share") {
        if (LaunchUtil.isInstallWeixin && !SettingUtil.isAppStoreAudit()) {
          ShareUtil.showShareSelect(
              context,
              pageStr ??
                  (Platform.isIOS ? SettingUtil.iDLDUrl : SettingUtil.aDLDUrl),
              shareTitle,
              shareContent,
              shareAvatar,
              shareTitle);
        }
        return;
      } else if (type == "ShareWXTimeLine") {
        if (LaunchUtil.isInstallWeixin && !SettingUtil.isAppStoreAudit()) {
          ShareUtil.shareWXTimeLine(
              pageStr ??
                  (Platform.isIOS ? SettingUtil.iDLDUrl : SettingUtil.aDLDUrl),
              shareTitle,
              shareAvatar,
              shareContent,
              shareTitle);
        }
        return;
      } else if (type == "ShareWXSession") {
        if (LaunchUtil.isInstallWeixin && !SettingUtil.isAppStoreAudit()) {
          ShareUtil.shareWXSession(
              pageStr ??
                  (Platform.isIOS ? SettingUtil.iDLDUrl : SettingUtil.aDLDUrl),
              shareTitle,
              shareAvatar,
              shareContent,
              shareTitle);
        }
        return;
      }
    }
    if (page != null) {
      Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
        return page;
      }));
      return;
    } else if (pageStr != null &&
        pageStr.isNotEmpty &&
        type != null &&
        (type == "app" || type == "Native")) {
      Navigator.of(context).pushNamed("/${pageStr}");
      return;
    }
    if (showTips != null && showTips.isNotEmpty) {
      ToastUtil.showMessageToast(showTips);
    }
  }
}
